package com.ssq.mobilehelper.aes;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ssq.mobilehelper.global.AppSettings;
import com.ssq.mobilehelper.global.MyApp;
import com.ssq.mobilehelper.R;
import com.ssq.mobilehelper.greendao.gen.EncryptBeanDao;
/**
 加密详细界面
 */
public class LookActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener, RatingBar.OnRatingBarChangeListener {

    private EditText mEditAccount;
    private EditText mEditPassword;
    private Spinner mSpinner;
    private RatingBar mRatingBar;
    private EditText mEditExtra;
    private Button mBtnSubmit;
    private   EncryptBean encryptBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look);
        initView();

        getInfo();

    }
//初始化
    private void initView() {
        mEditAccount = (EditText) findViewById(R.id.editAccount);
        mEditPassword = (EditText) findViewById(R.id.editPassword);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
        mEditExtra = (EditText) findViewById(R.id.editExtra);
        mBtnSubmit = (Button) findViewById(R.id.btn_submit);

        mBtnSubmit.setOnClickListener(this);

        mEditAccount.setFocusableInTouchMode(false);
        mEditAccount.setFocusable(false);
        mSpinner.setOnItemSelectedListener(this);
        mEditPassword.setFocusable(false);
        mEditPassword.setFocusableInTouchMode(false);
        mRatingBar.setFocusable(false);
        mRatingBar.setFocusableInTouchMode(false);
        mEditExtra.setFocusable(false);
        mEditExtra.setFocusableInTouchMode(false);
        mRatingBar.setIsIndicator(true);
        mSpinner.setEnabled(false);
        mRatingBar.setOnRatingBarChangeListener(this);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:

                if(mBtnSubmit.getText().equals("修改"))
                {
                    //TODO 修改密码
                    mBtnSubmit.setText("保存");

                    mEditAccount.setFocusableInTouchMode(true);
                    mEditAccount.setFocusable(true);
                    mEditAccount.requestFocus();

                    mEditPassword.setFocusable(true);
                    mEditPassword.setFocusableInTouchMode(true);
                    mRatingBar.setFocusable(true);
                    mRatingBar.setFocusableInTouchMode(true);
                    mEditExtra.setFocusable(true);
                    mEditExtra.setFocusableInTouchMode(true);
                    mRatingBar.setIsIndicator(false);
                    mSpinner.setEnabled(true);

                }
                else if (mBtnSubmit.getText().equals("保存"))
                {
                    //TODO 保存密码
                    if(TextUtils.isEmpty(mEditAccount.getText().toString()))
                    {
                        return;
                    }
                    if(TextUtils.isEmpty(mEditPassword.getText().toString()))
                    {
                        return;
                    }
                    encryptBean.setLevel(mRatingBar.getNumStars());
                    encryptBean.setMessage(mEditExtra.getText().toString());
                    encryptBean.setAccount(mEditAccount.getText().toString());

                    String temp = null;
                    try {
                         temp = AESUtils.encrypt(AppSettings.PASSWORD,mEditPassword.getText().toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    encryptBean.setPassword(temp);
                    encryptBean.setLevel(mRatingBar.getRating());

                    EncryptBeanDao dao = MyApp.getInstances().getDaoSession().getEncryptBeanDao();
                    dao.update(encryptBean);
                    Toast.makeText(this, "已修改", Toast.LENGTH_SHORT).show();



                    mEditAccount.setFocusableInTouchMode(false);
                    mEditAccount.setFocusable(false);

                    mEditPassword.setFocusable(false);
                    mEditPassword.setFocusableInTouchMode(false);
                    mRatingBar.setFocusable(false);
                    mRatingBar.setFocusableInTouchMode(false);
                    mEditExtra.setFocusable(false);
                    mEditExtra.setFocusableInTouchMode(false);
                    mRatingBar.setIsIndicator(true);
                    mSpinner.setEnabled(false);
                    mBtnSubmit.setText("修改");

                }
                break;
        }
    }

    public void getInfo()  {
        Intent intent = getIntent();
        String str = intent.getStringExtra("intent");
        Gson gson = new Gson();
         encryptBean = gson.fromJson(str,EncryptBean.class);

        mEditAccount.setText(encryptBean.getAccount());

        // 解密
        String temp = null;
        try {
            temp = AESUtils.decrypt(AppSettings.PASSWORD,encryptBean.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mEditPassword.setText(temp);
        mEditExtra.setText(encryptBean.getMessage());
        mRatingBar.setRating(encryptBean.getLevel());


        String[] arrays = getResources().getStringArray(R.array.languages);
        for(int i=0;i<arrays.length;i++)
        {
            if(encryptBean.getType().equals(arrays[i]))
            {
                mSpinner.setSelection(i,true);
            }
        }



    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String[] arrays = getResources().getStringArray(R.array.languages);
        encryptBean.setType(arrays[i]);

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {


    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
       ratingBar.setRating(v);
    }
}
